package com.cyxu.moviedigest.activities;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.cyxu.moviedigest.R;
import com.cyxu.moviedigest.data.AppConstant;

public class RegisterActivity extends ActionBarActivity {

    private EditText et_username;
    private EditText et_password;
    private EditText et_confirm;
    private String username;
    private String password;
    private String confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        et_username = (EditText) findViewById(R.id.et_register_username);
        et_password = (EditText) findViewById(R.id.et_register_password);
        et_confirm = (EditText) findViewById(R.id.et_register_confirm);

        Button btn_register = (Button) findViewById(R.id.btn_register);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attemptRegister();
            }
        });

        TextView tv_loginOption = (TextView) findViewById(R.id.tv_register_login);
        tv_loginOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                returnLogin();
            }
        });

    }

    public void attemptRegister() {
        username = et_username.getText().toString();
        password = et_password.getText().toString();
        confirm = et_confirm.getText().toString();

        if(!password.equals(confirm)) {
            Toast.makeText(this, "Password not match!", Toast.LENGTH_SHORT).show();
        }
        else {
            String url = AppConstant.SERVER_URL + "/regist?username=" + username + "&password=" + password;
            Log.v("URL", url);
            RequestQueue queue = Volley.newRequestQueue(this);

            StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    Toast.makeText(getBaseContext(), "Successfully registered! Now you can login:)", Toast.LENGTH_SHORT).show();
                    returnLogin();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    if (volleyError.networkResponse != null) {
                        int status = volleyError.networkResponse.statusCode;
                        Log.e("HTTP_RESPONSE", Integer.toString(status));
                    }
                    Toast.makeText(getBaseContext(), "The username already exists.", Toast.LENGTH_SHORT).show();
                }
            });

            queue.add(stringRequest);
        }
    }

    public void returnLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
